package com.auerdi.ui.detail.cutprice.child.view;

import android.content.Context;
import android.util.AttributeSet;

import com.example.egghttp.EggLog.L;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;


/**
 * Created by highsixty on 2018/3/14.
 * mail  gaolulin@sunmi.com
 */

public class ClockTextV2 extends android.support.v7.widget.AppCompatTextView {
    //    private Timer timer;
    private Integer condownSecond = 0;
    private Integer timesta;
    private Subscription eew;


    public void setCondownSecond(Integer condownSecond, int timesta) {
//        L.d("hcia", "ClockTextV2 setCondownSecond:" + " condownSecond:" + condownSecond + " timesta:" + timesta);
        this.condownSecond = condownSecond;
        this.timesta = timesta;

        eew = Observable.interval(0, 1, TimeUnit.SECONDS)//设置0延迟，每隔一秒发送一条数据
                .take(condownSecond + 1) //设置循环11次
                .map(aLong -> condownSecond - aLong)
                .observeOn(AndroidSchedulers.mainThread())//操作UI主要在UI线程
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        L.d("hcia", "e:" + e);
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Long aLong) { //接受到e一条就是会操作一次UI

//                                        secondTxt.setText();


                        Long nowsta = System.currentTimeMillis() / 1000;
                        long condow = timesta + condownSecond - nowsta;

                        long hh = condow / 3600;
                        long mm = (condow - hh * 3600) / 60;
                        long ss = condow - hh * 3600 - mm * 60;


//                            secondTxt.setText("" + ss + "");
//                            minuteTxt.setText("" + mm + "");
//                            hourTxt.setText("" + hh + "");

                        ClockTextV2.this.post(() -> {
//                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//                        String dateString = simpleDateFormat.format(new Date());
//                        L.d("hcia", "dateString:" + dateString);
//                        ClockText.this.setText(dateString);
                            setText(hh + ":" + mm + ":" + ss);

                        });
//                                        btnCheck.setTextColor(Color.BLACK);
                    }
                });

    }


    public ClockTextV2(Context context) {
        super(context);
        init(context);
    }


    public ClockTextV2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public ClockTextV2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);


    }


    private void init(Context context) {
//        L.d("hcia", "context:" + context);


    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
//        L.d("hcia", "ClockTextV2 onAttachedToWindow:" + " condownSecond:" + condownSecond);

    }

    @Override
    protected void onDetachedFromWindow() {
//        L.d("hcia", "ClockTextV2 onDetachedFromWindow:" + " condownSecond:" + condownSecond);
        super.onDetachedFromWindow();
        if (eew != null)
            eew.unsubscribe();
    }
}
